package org.etom.impl.ui.common;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.etom.connector.datatypes.IMoodleConnection;
import org.etom.connector.datatypes.IMoodleCourse;
import org.etom.connector.datatypes.IMoodleModule;
import org.etom.connector.service.ConnectorException;
import org.etom.impl.ui.Activator;
import org.etom.impl.ui.util.CommonMessages;
import org.etom.impl.ui.util.Logger;
import org.etom.impl.ui.util.Utility;

/**
 * The common label and content provider.
 */
class ResourcesTreeProvider implements ITreeContentProvider, ILabelProvider {

  private static final Object[] EMPTY_ARRAY = {};
  
  private Object rootElement;
  private ResourcesDataProvider resDataProvider;
  private int resourceType;
  
  private Image moodleConnection;
  private Image moodleCourse;
  private Image moodleAssignment;
  private Image moodleResource;
  
  /**
   * Constructs the new resource tree provider.
   * 
   * @param treeViewer - the tree viewer
   * @param type - the modules type which will be provided
   */
  public ResourcesTreeProvider(Object rootElement,
      ResourcesDataProvider resDataProvider, int type) {
    if ((type != IMoodleModule.ASSIGNMENT)
     && (type != IMoodleModule.RESOURCE)) {
      throw new  IllegalArgumentException(CommonMessages.Invalid_Type);
    }
    this.rootElement = rootElement;
    this.resDataProvider = resDataProvider;
    this.resourceType = type;
  }
  
  /*===[Content Provider][Start]===*/
  @Override
  public void dispose() {
    if (null == moodleConnection) return;
    moodleConnection.dispose();
    moodleConnection = null;
    moodleAssignment.dispose();
    moodleAssignment = null;
    moodleCourse.dispose();
    moodleCourse = null;
    moodleResource.dispose();
    moodleResource = null;
  }

  /**
   * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
   */
  @Override
  public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    //do nothing here
  }

  /**
   * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
   */
  @Override
  public Object[] getChildren(Object parentElement) {
    try {
      if (parentElement == rootElement) {
        return resDataProvider.getMoodleConnections().toArray();
      } else if (parentElement instanceof IMoodleConnection) {
        return ((IMoodleConnection) parentElement).getUser().getCourses();
      } else if (parentElement instanceof IMoodleCourse) {
        return ((IMoodleCourse) parentElement).getModules(resourceType);
      }
    } catch (ConnectorException ce) {
      Logger.ERROR(
          CommonMessages.Access_Children_Fail, ce);
    }
    return EMPTY_ARRAY;
  }

  /**
   * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
   */
  @Override
  public Object getParent(Object element) {
    return null;
  }

  /**
   * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
   */
  @Override
  public boolean hasChildren(Object element) {
    if (element instanceof IMoodleModule) return false;
    return true;
  }

  /**
   * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
   */
  @Override
  public Object[] getElements(Object inputElement) {
    return getChildren(inputElement);
  }
  /*===[Content Provider][End]===*/
  
  /*===[Label Provider][Start]===*/
  @Override
  public Image getImage(Object element) {
    if (null == moodleConnection) {
      moodleConnection = ImageDescriptor.createFromFile(
          Activator.class, IconConstants.MOODLE_CONNECTION_ICON_PATH).createImage();
      moodleAssignment = ImageDescriptor.createFromFile(
          Activator.class, IconConstants.MOODLE_ASSIGNMENT_ICON_PATH).createImage();
      moodleCourse = ImageDescriptor.createFromFile(
          Activator.class, IconConstants.MOODLE_COURSE_ICON_PATH).createImage();
      moodleResource = ImageDescriptor.createFromFile(
          Activator.class, IconConstants.MOODLE_RESOURCE_ICON_PATH).createImage();
    }
    if (element instanceof IMoodleConnection) {
      return moodleConnection;
    } else if (element instanceof IMoodleCourse) {
      return moodleCourse;
    } else if (element instanceof IMoodleModule) {
      switch (((IMoodleModule) element).getType()) {
      case IMoodleModule.RESOURCE :
        return moodleResource;
      case IMoodleModule.ASSIGNMENT :
        return moodleAssignment;
      }
    }
    return null;
  }

  /**
   * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
   */
  @Override
  public String getText(Object element) {
    if (element == rootElement) return null;
    if (element instanceof IMoodleConnection)
      return Utility.toString( (IMoodleConnection) element);
    else if (element instanceof IMoodleCourse)
      return Utility.toString( (IMoodleCourse) element);
    else if (element instanceof IMoodleModule)
      return Utility.toString((IMoodleModule) element);
    return element.toString();
  }

  /**
   * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
   */
  @Override
  public void addListener(ILabelProviderListener listener) {
    //do nothing here
  }

  /**
   * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String)
   */
  @Override
  public boolean isLabelProperty(Object element, String property) {
    return false;
  }

  /**
   * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
   */
  @Override
  public void removeListener(ILabelProviderListener listener) {
    //do nothing here
  }
  /*===[Label Provider][End]===*/

}
